//
//  MutexDemo.m
//  iOSLock
//
//  Created by 崔博 on 2018/7/24.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "MutexDemo.h"

#import <pthread.h>

@interface MutexDemo()

@property (nonatomic, assign) pthread_mutex_t tickMutex;
@property (nonatomic, assign) pthread_mutex_t moneyMutex;
@end

@implementation MutexDemo

- (void)__initMute:(pthread_mutex_t *)mutex
{
    // 初始化锁属性
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_setcanceltype(&attr, PTHREAD_MUTEX_DEFAULT);
    
    // 初始化锁
    pthread_mutex_init(&mutex, &attr);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self __initMute:&_tickMutex];
        [self __initMute:&_moneyMutex];
     
        
    }
    return self;
}


- (void)__saleTickets
{
    pthread_mutex_lock(&_tickMutex);
    [super __saleTickets];
    pthread_mutex_unlock(&_moneyMutex);
    
}
- (void)__saveMoney
{
    pthread_mutex_lock(&_moneyMutex);
    [super __saveMoney];
    pthread_mutex_unlock(&_moneyMutex);
}
- (void)__drawMoney
{
    pthread_mutex_unlock(&_moneyMutex);
    [super __drawMoney];
    pthread_mutex_unlock(&_moneyMutex);
}

@end
